package cn.wolfcode.wolfcodemall.service.impl;

import cn.wolfcode.wolfcodemall.domain.Category;
import cn.wolfcode.wolfcodemall.mapper.CategoryMapper;
import cn.wolfcode.wolfcodemall.qo.CategoryQo;
import cn.wolfcode.wolfcodemall.qo.PageQo;
import cn.wolfcode.wolfcodemall.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    public List<Category> findAll() {
        return categoryMapper.selectAll();
    }

    public PageInfo<Category> findByPage(PageQo qo) {
        //通过PageHelper实现分页
        //为PageHelper设置分页信息
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //执行查询sql
        List<Category> categoryList = categoryMapper.selectAll();

        return new PageInfo<Category>(categoryList);
    }

    public PageInfo<Category> findByCondition(CategoryQo qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //执行sql
        List<Category> categoryList = categoryMapper.selectByCondition(qo);
        return new PageInfo<Category>(categoryList);
    }

    @Transactional
    public void saveOrUpdate(Category category, String propertys) {
        //区分当前操作是添加还是修改操作
        //如果分类id为null，
        if(category.getCategoryId() == null){
            //当前添加操作
            //保存分类信息
            categoryMapper.insert(category);
        }else{
            //修改操作
            //修改分类信息
            categoryMapper.updateByPrimaryKey(category);
            //删除该分类下所有的属性信息
            categoryMapper.deletePropertyByCategoryId(category.getCategoryId());
            //在将新的信息的属性添加到数据库中
        }

        //关联保存分类的属性信息   保存到property表中
        //从propertys拆分出每一个属性，将每一个属性保存
        if(propertys != null && !propertys.equals("")){
            String[] split = propertys.split(",");
            for (String property : split) {
                categoryMapper.insertCategoryProperty(property,category.getCategoryId());
            }
        }
    }

    public Category findById(Integer categoryId) {
        return categoryMapper.selectByPrimaryKey(categoryId);
    }

    public List<String> findPropertyNameByCategoryId(Integer categoryId) {
        return categoryMapper.selectPropertyNameByCategoryId(categoryId);
    }

    @Transactional
    public void delete(Integer categoryId) {
        //级联删除，当删除分类时，关联删除依赖于该分类的所有商品信息
        //为了保证数据安全性，在删除分类前，检查是否有其他数据(商品)依赖于该分类
        List<Integer> pids = categoryMapper.selectProductIdByCategoryId(categoryId);
        //如果有商品依赖该分类，不能删除该分类
        if(pids.size() > 0){
            //抛出一个异常
            throw new RuntimeException(pids.toString());
        }

        //根据id删除分类信息
        categoryMapper.deleteByPrimaryKey(categoryId);
        //关联删除分类的属性信息
        categoryMapper.deletePropertyByCategoryId(categoryId);
    }

//    @Override
//    public List<Category> selectByCategoryId(Integer categoryId) {
//        return categoryMapper.selectByCategoryId(categoryId);
//    }
}
